//
//  ArtclePublishViewController.m
//  reSearchDemo
//
//  Created by 云库 on 15/10/27.
//  Copyright (c) 2015年 Kiwaro. All rights reserved.
//

#import "ArtclePublishViewController.h"
#import "CameraActionSheet.h"
#import "VPImageCropperViewController.h"

@interface ArtclePublishViewController ()<UITextFieldDelegate,UITextViewDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate,CameraActionSheetDelegate>
@property (weak, nonatomic) IBOutlet UITextField *headTitle;
@property (weak, nonatomic) IBOutlet UITextView *Content;

@end

@implementation ArtclePublishViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"文章发布";
    
    self.headTitle.delegate=self;
    self.Content.delegate=self;
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"发布" style:UIBarButtonItemStyleDone target:self action:@selector(releaseBtnAction:)];
}

#pragma mark --发布按钮点击事件--
-(void)releaseBtnAction:(UIBarButtonItem *)sender {
    
}

#pragma mark--上传图片按钮
- (IBAction)UploadPhoto:(UIButton *)sender {
    CameraActionSheet *actionSheet = [[CameraActionSheet alloc] initWithActionTitle:nil TextViews:nil CancelTitle:@"取消" withDelegate:self otherButtonTitles:@"从相册选择", @"拍一张", nil];
    [actionSheet show];
//    SelectBtn = sender;
}

#pragma mark -- CameraActionSheetDelegate --
- (void)cameraActionSheet:(CameraActionSheet *)sender didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 2) {
        return;
    }
    UIImagePickerController * picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    if (buttonIndex == 0){
        picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    } else if (buttonIndex == 1) {
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
            
            picker.sourceType = UIImagePickerControllerSourceTypeCamera;
        } else {
            //                [self showText:@"无法打开相机"];
        }
    }

    [self presentViewController:picker animated:YES completion:nil];

}
#pragma mark -- UIImagePickerControllerDelegate --
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    [picker dismissViewControllerAnimated:YES completion:^() {
        UIImage * img = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
        img = [UIImage rotateImage:img];
        VPImageCropperViewController *imgCropperVC = [[VPImageCropperViewController alloc] initWithImage:img cropFrame:CGRectMake((self.view.frame.size.width - 240)/2, 100.0f, 240, 400) limitScaleRatio:3.0 title:@"上传素材"];
        [imgCropperVC setCompletionBlock:^(BOOL didFinished, UIImage *editedImage) {
            if (editedImage) {

                NSLog(@"hahah");
                
                NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithAttributedString:self.Content.attributedText];
                
                NSTextAttachment *textAttachment = [[NSTextAttachment alloc] initWithData:nil ofType:nil] ;
                
                textAttachment.image = editedImage; //要添加的图片
                
                NSAttributedString *textAttachmentString = [NSAttributedString attributedStringWithAttachment:textAttachment] ;
                UITextRange *range=self.Content.selectedTextRange;
                
                [string insertAttributedString:textAttachmentString atIndex:range];//index为用户指定要插入图片的位置
                self.Content.attributedText = string;
//                self.Content.text=@"1111";
                
            }
        }];
        [self.navigationController pushViewController:imgCropperVC animated:YES];
    }];
}
#pragma mark--选择分类按钮
- (IBAction)Classify:(UIButton *)sender {
}

#pragma mark--回收键盘
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}

- (void)textViewDidEndEditing:(UITextView *)textView{
    [self.Content resignFirstResponder];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
